package com.kkb.controller;

import com.github.pagehelper.PageInfo;
import com.kkb.pojo.Team;
import com.kkb.pojo.TeamAndPageInfo;
import com.kkb.service.TeamService;
import com.kkb.vo.TeamConditionParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

@Controller
@RequestMapping("team")
public class TeamController  {

    @Resource
    private TeamService teamService;

    //多条件分页查询
    @ResponseBody
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public TeamAndPageInfo<Team> findByPage(Integer pageNum, Integer pageSize, TeamConditionParam param){
        if (pageNum == null || pageNum <= 0){
            pageNum = 1;
        }

        if (pageSize == null || pageSize <= 0){
            pageSize = 5;
        }

        PageInfo<Team> teamPageInfo = teamService.findByPage(pageNum,pageSize,param);

        return new TeamAndPageInfo<>(teamPageInfo);

    }

    //添加球队
    @ResponseBody
    @RequestMapping(method = RequestMethod.POST)
    public TeamAndPageInfo<Team> addTeam(Team team){
        int i = teamService.addTeam(team);
        if (i==1){
            return new TeamAndPageInfo<>(1,200,"球队信息添加成功~");
        }

        return new TeamAndPageInfo<>(-1,500,"服务器内部异常，请稍后再试！");
    }

    //主键查询球队信息
    @ResponseBody
    @RequestMapping(value = "{id}",method = RequestMethod.GET)
    public TeamAndPageInfo<Team> findById(@PathVariable("id") Integer teamId){

        Team team = teamService.findById(teamId);

        return new TeamAndPageInfo<>(team);

    }

    //更新球队信息，根据主键更新
    @ResponseBody
    @RequestMapping(value = "{id}",method = RequestMethod.PUT)
    public TeamAndPageInfo<Team> updateById(@PathVariable("id") Integer teamId,Team team){
        team.setTeamId(teamId);

        int i = teamService.updateById(team);
        if (i==1){
            return new TeamAndPageInfo<>(1,200,"球队信息更新成功~");
        }

        return new TeamAndPageInfo<>(-1,500,"服务器内部异常，请稍后再试！");
    }

    //根据主键删除
    @ResponseBody
    @RequestMapping(value ="{id}",method = RequestMethod.DELETE)
    public TeamAndPageInfo<Team> deleteById(@PathVariable("id") Integer teamId){

        int i = teamService.deleteById(teamId);
        if (i==1){
            return new TeamAndPageInfo<>();
        }

        return new TeamAndPageInfo<>(-1,500,"服务器内部异常，请稍后再试！");
    }

    //文件上传
    @RequestMapping(value = "{id}",method = RequestMethod.POST)
    public TeamAndPageInfo<Team> uploadLogo(@RequestParam("logo") MultipartFile myFile, HttpServletRequest request,
                                            @PathVariable("id") Integer teamId){
        //文件上传到指定服务器位置
        String path = request.getServletContext().getRealPath("/img/uploadFile/");
        //获取原始文件名称
        String originalFileName = myFile.getOriginalFilename();
        //定义文件的新名称：随机生成的名称+原有文件的后缀名
        String randomName = UUID.randomUUID().toString().replace("-","");
        int index = originalFileName.lastIndexOf(".");
        String hz = originalFileName.substring(index);
        String logoName = randomName + hz;

        int num = 0;

        try {
            //将内存中图片写入磁盘
            myFile.transferTo(new File(path + "\\" + logoName));
            System.out.println("上传成功！"+path + "\\" + logoName);
            //将图片的名称更新到数据库中
            Team team = new Team();
            team.setTeamId(teamId);
            team.setTeamLogo(logoName);
            num = teamService.updateById(team);

            if (num == 1){
                return new TeamAndPageInfo<>(1,200,"图片上传成功~");
            }else {
                return new TeamAndPageInfo<>(-1,500,"服务端内部有误，请修复！");
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new TeamAndPageInfo<>(-1,500,"图片上传出错！" + e.getMessage());
        }


    }
}
